/**
 * Api定义抽象类
 */
import http from '@ohos.net.http'
export default abstract class api {
  url:string
  query:Map<string,string>
  body:object
  method:http.RequestMethod=http.RequestMethod.GET
  public static async getInstance():Promise<any>{
    throw new Error("Method Not Implement")
  }
  protected constructor(params:{url:string,body?:object,query?:Map<string,string>|object,method?:http.RequestMethod}) {
    this.url = params.url
    if(params.body !== null && params.body !== undefined){
      this.body = params.body
    }
    if(params.query !== null && params.query !== undefined){
      if(params.query instanceof Map){
        this.query = params.query
      }else{
        this.query = new Map(Object.entries(params.query))
      }
    }
    if(params.method !== null && params.method !== undefined){
      this.method = params.method
    }
  }
  getQueryStr(){
    if(this.query === undefined || this.query === null || this.query.size === 0){
      return ''
    }
    let qs = ''
    this.query.forEach((value,key)=>{
      qs += `${key}=${value}&`
    })
    return qs.slice(0,-1)
  }
  getBodyStr(){
    if(this.body === undefined || this.body === null)
      return ''
    return JSON.stringify(this.body)
  }
}